<script setup>
import { ref } from 'vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { formatTime } from '@/utils/format'

const articleList = ref([]) // 文章列表
const total = ref(0) // 文章总条数，默认值是0
const loading = ref(false) // loading 状态

const params = ref({
  pagenum: 1, // 当前页
  pagesize: 5, // 每页显示条数
  cate_id: null,
  state: null
})

const getArticleList = () => {
  loading.value = true
  // 模拟 API 调用  const res = await artGetListService(params.value)
  //  articleList.value = res.data.data
  // total.value = res.data.total
  setTimeout(() => {
    // 模拟获取文章数据
    articleList.value = [
      {
        id: 1,
        title: 'Vue 3 新特性',
        cate_name: '前端',
        pub_date: '2023-01-01',
        state: '已发布'
      },
      {
        id: 2,
        title: 'Node.js 高性能编程',
        cate_name: '后端',
        pub_date: '2023-01-03',
        state: '草稿'
      }
    ]
    total.value = articleList.value.length
  }, 1000)

  loading.value = false
}
getArticleList()

const onSearch = () => {
  console.log('搜索', params.value)
}

const onReset = () => {
  params.value = {
    cate_id: null,
    state: null
  }
}

const onEditArticle = (row) => {
  console.log('编辑文章', row)
  // 跳转到编辑页面
  // $router.push(`/article/edit/${row.id}`)
}

const onDeleteArticle = (row) => {
  ElMessageBox.confirm('此操作将永久删除该文章, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      console.log('删除文章', row)
    })
    .catch(() => {
      console.log('取消删除')
    })
}
const onAddArticle = () => {
  console.log('添加文章')
  // 跳转到添加页面
  // $router.push('/article/add')
}

const onSizeChange = (size) => {
  // 只要是每页的条数发生了变化，那么原本正在访问的当前页意义不大了，数据大概已经不在当前页了。
  // 重新从第一页渲染即可。
  params.value.pagenum = 1
  params.value.pagesize = size
  // 基于最新的当前页和每页条数，渲染数据。
  getArticleList()
}

const onCurrentChange = (page) => {
  // 更新当前页
  params.value.pagenum = page
  // 基于最新的当前页渲染数据
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">新增文章 </el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="文章分类:">
        <!--
          ChannelSelect 组件
            vue2 => v-model :value 和 @input 的简写
            vue3 => v-model :modelValue 和 @update:modelValue 的简写
        -->
        <channel-select v-model="params.cate_id" :width="'15rem'" />
      </el-form-item>
      <el-form-item label="发布状态:">
        <el-select v-model="params.state" style="width: 15rem" size="large">
          <el-option label="已发布" value="1"></el-option>
          <el-option label="草稿" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="onSearch" type="primary">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" prop="title">
        <!-- 作用域插槽 -->
        <template #default="{ row }">
          <el-link type="primary" underline="never">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作">
        <!-- 作用域插槽，这里需要进行文章删除，修改需要用到 id，所以采用作用域插槽。 -->
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            plain
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            type="danger"
            :icon="Delete"
            circle
            plain
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </page-container>
</template>

<style lang="scss" scoped></style>
